package cn.online.taxi.order.service.service.impl;

import cn.online.taxi.common.constant.RedisKeyConstant;
import cn.online.taxi.common.exception.OnlineTaxiException;
import cn.online.taxi.common.jedis.JedisClient;
import cn.online.taxi.driver.user.service.service.DriverInfoService;
import cn.online.taxi.manager.dto.front.Order;
import cn.online.taxi.manager.mapper.TbDriverMapper;
import cn.online.taxi.manager.mapper.TbOrderMapper;
import cn.online.taxi.manager.pojo.TbDriver;
import cn.online.taxi.manager.pojo.TbOrder;
import cn.online.taxi.order.service.service.OrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author BinBini
 * @Date 2021/4/7 10:00
 * @Version 1.0
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private TbOrderMapper tbOrderMapper;
    @Resource
    private TbDriverMapper tbDriverMapper;
    @Reference
    private DriverInfoService driverInfoService;
    @Autowired
    private JedisClient jedisClient;
    @Value("100000")
    private Integer ORDER_RECEIVING_EXPIRE;
    @Value("100000")
    private Integer ORDER_START_EXPIRE;

    @Override
    public boolean grab(Integer orderId, long driverId) {
        TbOrder tbOrder = tbOrderMapper.selectByPrimaryKey(orderId);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (tbOrder.getStatus() == 0) {
            tbOrder.setStatus(1);
            if (tbOrderMapper.updateByPrimaryKey(tbOrder) != 1) {
                throw new OnlineTaxiException("更新订单状态码失败");
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean receiving(String orderNumber, Integer driverId) {
        TbDriver tbDriver = tbDriverMapper.selectByPrimaryKey(driverId);
        if (tbDriver == null) {
            throw new OnlineTaxiException("没有该司机");
        }
        String orderJSON = jedisClient.get(RedisKeyConstant.ORDER_KEY_PRE + ":" + orderNumber);
        Order order = new Gson().fromJson(orderJSON, Order.class);
        if (order.getDriverId() != driverId) {
            return false;
        }
        // 司机去接乘客, 修改订单状态
        order.setDriverStartTime(new Date());
        order.setDriverStatus(2);
        order.setStatus(3);
        // 接单信息写入redis, key: "RECEIVING_LOCK_ORDER:orderNumber" value: "order"
        jedisClient.set(RedisKeyConstant.ORDER_KEY_PRE + ":" + order.getOrderNumber(), new Gson().toJson(order));
        jedisClient.expire(RedisKeyConstant.ORDER_KEY_PRE + ":" + order.getOrderNumber(), ORDER_RECEIVING_EXPIRE);
        return true;
    }

    @Override
    public boolean startJourney(String orderNumber, Integer driverId, String phone, String receivePassengerLongitude, String receivePassengerLatitude) {
        TbDriver tbDriver = tbDriverMapper.selectByPrimaryKey(driverId);
        if (tbDriver == null) {
            throw new OnlineTaxiException("没有该司机");
        }
        String orderJSON = jedisClient.get(RedisKeyConstant.ORDER_KEY_PRE + ":" + orderNumber);
        Order order = new Gson().fromJson(orderJSON, Order.class);
        if (order.getDriverId() != driverId) {
            return false;
        }
//        String lastPhone = order.getPassengerPhone().substring(order.getPassengerPhone().length()-4, order.getPassengerPhone().length());
//        if (!lastPhone.equals(phone)) {
//            return false;
//        }
        // 乘客上车，司机开始行程， 修改订单状态
        order.setReceivePassengerTime(new Date());
        order.setReceivePassengerLongitude(receivePassengerLongitude);
        order.setReceivePassengerLatitude(receivePassengerLatitude);
        order.setOrderStartTime(new Date());
        order.setDriverStatus(4);
        order.setStatus(5);
        // 订单信息写入redis, key: "RECEIVING_LOCK_ORDER:orderNumber" value: "order"
        jedisClient.set(RedisKeyConstant.ORDER_KEY_PRE + ":" + order.getOrderNumber(), new Gson().toJson(order));
        jedisClient.expire(RedisKeyConstant.ORDER_KEY_PRE + ":" + order.getOrderNumber(), ORDER_START_EXPIRE);
        return true;
    }

    @Override
    public boolean endJourney(String orderNumber, Integer driverId, String passengerGetoffLongitude, String passengerGetoffLatitude) {
        TbDriver tbDriver = tbDriverMapper.selectByPrimaryKey(driverId);
        if (tbDriver == null) {
            throw new OnlineTaxiException("没有该司机");
        }
        String orderJSON = jedisClient.get(RedisKeyConstant.ORDER_KEY_PRE + ":" + orderNumber);
        Order order = new Gson().fromJson(orderJSON, Order.class);
        if (order.getDriverId() != driverId) {
            return false;
        }
        // 结束行程， 修改订单状态
        order.setPassengerGetoffTime(new Date());
        order.setPassengerGetoffLongitude(passengerGetoffLongitude);
        order.setPassengerGetoffLatitude(passengerGetoffLatitude);
        order.setDriverStatus(5);
        order.setStatus(6);
        // 写入redis
        jedisClient.set(RedisKeyConstant.ORDER_KEY_PRE + ":" + orderNumber, new Gson().toJson(order));
        return true;
    }

    @Override
    public boolean charge(String orderNumber, Integer driverId) {
        TbDriver tbDriver = tbDriverMapper.selectByPrimaryKey(driverId);
        if (tbDriver == null) {
            throw new OnlineTaxiException("没有该司机");
        }
        String orderJSON = jedisClient.get(RedisKeyConstant.ORDER_KEY_PRE + ":" + orderNumber);
        Order order = new Gson().fromJson(orderJSON, Order.class);
        if (order.getDriverId() != driverId) {
            return false;
        }
        // 发起收费， 修改订单状态
        order.setDriverStatus(6);
        order.setStatus(7);
        order.setIsPaid(0);
        // 写入redis
        jedisClient.set(RedisKeyConstant.ORDER_KEY_PRE + ":" + orderNumber, new Gson().toJson(order));
        return true;
    }

    @Override
    public Order getOrderRedisByNumber(String orderNumber) {
        String orderJSON = jedisClient.get(RedisKeyConstant.ORDER_KEY_PRE + ":" + orderNumber);
        Order order = new Gson().fromJson(orderJSON, Order.class);
        return order;
    }

}
